package com.uchedao.seller.ui.car.publish.vin.http;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by ZhangZhanghua on 2015/8/25 0025.
 */
public class AsyncHttpBaseAPI {

    public static AsyncHttpBaseAPI BaseAPIInstance = null;
    private AsyncHttpClient client = new AsyncHttpClient();
    private boolean receiverNetwordChanged;
    private NetworkChangeReceiver receiver;

    private Context mContext;

    public AsyncHttpBaseAPI(Context context) {
        this(context, false);
    }

    /**
     * 是否监听网络状态
     *
     * @param networkChangeReceive
     */
    public AsyncHttpBaseAPI(Context context, boolean networkChangeReceive) {
        BaseAPIInstance = this;
        mContext = context;
        receiverNetwordChanged = networkChangeReceive;
        if (receiverNetwordChanged)
            registerNetwordChangeReceiver();

    }

    /**
     * 注册网络监听
     *
     * @author 叶晋
     * @修改时间 2012-12-29 下午2:49:43
     *
     */
    private void registerNetwordChangeReceiver() {
        if (receiver == null)
            receiver = new NetworkChangeReceiver();

        mContext.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    public class NetworkChangeReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                // L.e(JsonUtils.toJson(DeviceUtils.getMobileNetworkInfo(context)));
                ConnectivityManager connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {
                    resetHttpClient();
                }
            }
        }

    }

    /**
     * 得到当前活动的线程数
     *
     * @return
     */
    public int getActiveCount() {
        if (client == null)
            return 0;
        return client.getActiveCount();
    }

    /**
     * 得到当前线程池最大容量
     *
     * @return
     */
    public int getMaxCount() {
        if (client == null)
            return 0;
        return client.getMaxCount();
    }

    /**
     * 释放
     *
     * @author 叶晋
     * @修改时间 2012-12-29 下午2:58:33
     *
     */
    public void finish() {
        if (receiverNetwordChanged)
            mContext.unregisterReceiver(receiver);
        if (client != null) {
            client.cancelRequests(mContext, true);
            client.shutdown();
            client = null;
        }
    }

    /**
     * 重置HTTP连接
     *
     * @author 叶晋
     * @修改时间 2012-12-29 下午2:50:52
     *
     */
    public void resetHttpClient() {
        if (client != null) {
            client.cancelRequests(mContext, true);
            client.shutdown();
        }
        client = new AsyncHttpClient();
    }

    public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(url, params, responseHandler);
    }

    public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(url, params, responseHandler);
    }

    public void get(String url, HeaderParams headerParams, RequestParams requestParams,
                    AsyncHttpResponseHandler responseHandler) {
        client.get(mContext, url, headerParams == null ? null : headerParams.getHeaders(), requestParams,
                responseHandler);
    }

    public void post(String url, HeaderParams headerParams, RequestParams requestParams,
                     AsyncHttpResponseHandler responseHandler) {
        client.post(mContext, url, headerParams == null ? null : headerParams.getHeaders(), requestParams, null,
                responseHandler);
    }

    public <T, E> void get(String url, RequestParams params, ModelHttpResponseHandler<T, E> responseHandler) {
        client.get(url, params, responseHandler);
    }

    public <T, E> void post(String url, RequestParams params, ModelHttpResponseHandler<T, E> responseHandler) {
        client.post(url, params, responseHandler);
    }

    public <T, E> void get(String url, HeaderParams headerParams, RequestParams requestParams,
                           ModelHttpResponseHandler<T, E> responseHandler) {
        client.get(mContext, url, headerParams == null ? null : headerParams.getHeaders(), requestParams,
                responseHandler);
    }

    public <T, E> void post(String url, HeaderParams headerParams, RequestParams requestParams,
                            ModelHttpResponseHandler<T, E> responseHandler) {
        client.post(mContext, url, headerParams == null ? null : headerParams.getHeaders(), requestParams, null,
                responseHandler);
    }
}
